Main content
Course: Intro to computer science - Python > Unit 2
Lesson 5: Making recommendationsInstructions: Recommendation engine
Apply conditionals to design a recommendation algorithm for a category of your choice.
The Internet is built on recommendation algorithms, from your social media feed to your music player app. Even Khan Academy has a recommendation engine for helping users find what to work on next! These systems gather data about a user’s attributes, behaviors, and preferences and use this data to match each user to content they think is most relevant to them.
In this project, you’ll build your own recommendation engine for a category you know a lot about.
Choosing a category
First, you need to choose something to make recommendations around. This could be anything from songs, to books, to recipes - it can even be as niche as dog breeds or football teams.
- Choose at least five different items within your category, and create a variable at the top of your program for each item.
These will be the items your recommendation algorithm selects from.
Collecting user data
Consider what user attributes or preferences might influence which of the items a user likes. For example, perhaps users who live in an urban area might prefer a small dog over a large dog.
- Use the
input()
function to ask the user at least three questions about their interests or characteristics.
Their answers will be the user data you use to guide your recommendation algorithm.
Designing an algorithm
Before you get to the code, start by thinking about what decision points you want to have. For example, should it branch first based on their favorite musical genre or based on their age? Consider drawing a control flow diagram to plan out your algorithm.
Then, translate your algorithm into code.
- Use conditionals to select a recommendation from among your variables, based on the user’s answers.
- Include at least two compound conditions, chained conditionals, or nested conditionals.
- Add comments to document any assumptions you make with your selections.
At the end of the program, you should have a final recommendation to print out for the user to see. Make sure that all possible paths through the program result in a single recommendation.
More to explore
Consider what assumptions or generalizations you made while developing your algorithm. Just like all algorithms, yours will represent a point of view. How might the choices you made influence your algorithm’s accuracy across different user groups?
Test your program with your friends and family, and see how they respond to the recommendation they get. Based on their feedback, try and refine your algorithm to produce more accurate results. You might need to adjust your decision points, change or add to the questions you ask, or increase the number of options. Programmers commonly work in build-test-refine loops like this to improve their projects in response to user feedback!
Finally, consider if you can refactor your program to make it easier for someone to interpret your algorithm. Can you make your code more readable or reduce the number of comparisons by reorganizing your conditions?
Want to join the conversation?
- Is this basically just computing but harder, and if so, how does it make it a difference apart from challenging your skills? Thanks to whoever helped me :)(4 votes)
- For the last challenge, I got an error message there were too many characters in a single line. Is there something we can insert to indicate to Python we want the code to include the next line(s)? Like in other software, I can type "///" at the end of a line to indicate the code continues to the next line.
I ended up shortening my line, but this is not ideal.(3 votes)- In Python, you can add a backslash to the end of a line to indicate it continues on the line below, or wrap the expression in parenthesis and Python will automatically continue parsing subsequent lines as part of a single line until the parenthesis is closed.(2 votes)
- So hold on a second, is this supposed to be somewhat similar to a For You Page (fyp)? Or more like a data collecter to figure out more about a user, or maybe both? I have no clue. Due to the course being brand new, I can't seem to understand a thing. I really hope they release videos soon like the JavaScript course(3 votes)
- how can we make our own python program?like i can make a java script one, but not a python one :((1 vote)
- There is currently no way on Khan Academy. This course is still being created though, so you may be able to write & run Python scripts here in the future.
[EDIT] You are now able to spin-off the Python courses! This isn't quite the same as creating your own project from scratch, but you can save a project to your profile now :)
[Another edit] It appears they removed the spin-off button :((4 votes)
- It says I can either extend upon the starter code or choose different topics and rewrite the starter code, but is there anything stopping me from scrapping the starter program entirely and writing my own code?(1 vote)
- Not at all! You're encouraged to get creative. Projects are designed in a way that gets you working with whatever was taught in previous lessons though, so I do recommend you try to implement some of the newer material (things you were just taught) to get familiar with it.(2 votes)